/**
 * 全局过滤器
*/
//订单状态
export function specialOrderStatus(val) {
    let list = ['', '待处理', '制作中', '待入库', '取消审核', '已取消', '待发货', '已发货'];
    return list[val]
}

export function staffType(val) {
    let list = ['', '轮钟', '点钟', '加班1轮钟', '加班1点钟', '加班2轮钟', '加班2点钟'];
    try {
        val = list[val];
    } catch (error) {
    }
    return val
}
//订单状态\r\n0: 作废\r\n1: 待结尾款\r\n2: 待消费\r\n3: 已完成\r\n4: 部分退款\r5:已退款 6:待完成
export function orderStatus(val) {
    let list = ['已作废', '待结尾款', '待消费', '已完成', '部分退款', '已退款', '待完成'];
    try {
        val = list[val];
    } catch (error) {

    }
    return val
}
//'状态 0：待审核 1：待出库 2：已驳回 3：已出库 4：已撤回 5：已入库'
export function allocateStatus(val) {
    let list = ['待审核', '待出库', '已驳回', '已出库', '已撤回', '已入库', '赠送次数抵扣'];
    try {
        val = list[val];
    } catch (error) {
    }
    return val
}
//会员详情=>赠送次数记录:类型
export function giveType(val) {
    let list = ['', '手动赠送', '消费赠送', '兑换项目赠送'];
    try {
        val = list[val];
    } catch (error) {

    }
    return val
}
export function orderType(val) {
    let list = ['未退款', '已退款', '退款中', '退款驳回'];
    try {
        val = list[val];
    } catch (error) {

    }
    return val
}

export function typeGender(val) {
    switch (Number(val)) {
        case 0:
            val = '保密';
            break;
        case 1:
            val = '男';
            break;
        case 2:
            val = '女';
            break;
        case -1:
            val = '条件限制';
            break;
    }
    return val
}

//员工状态  '状态 1：正常 0：禁用 -1：删除'
export function staffFilter(val) {
    switch (+val) {
        case -1:
            return '注销';
        case 0:
            return '禁用';
        case 1:
            return '正常';
    }
    return val
}
//会员来源
export function sourceTxt(val) {
    switch (+val) {
        case 8:
            val = "老系统数据";
            break;
        case 1:
            val = "朋友推荐";
            break;
        case 2:
            val = "老顾客推荐";
            break;
        case 3:
            val = "顾客来店";
            break;
        case 4:
            val = "其他";
            break;
        case 5:
            val = "小程序";
            break;
        case 6:
            val = "抖音";
            break;
        case 7:
            val = "美团";
            break;
    }
    return val;
}
/**
* Created by PanJiaChen on 16/11/18.
*/
export function typeTitle(val) {
    switch (Number(val)) {
        case 1:
            val = '详情';
            break;
        case 2:
            val = '新增';
            break;
        case 3:
            val = '修改';
            break;
    }
    return val
}

export function reserveStatus(val) {
    let list = ['取消预约', '待到店', '确认到店']
    try {
        val = list[val];
    } catch (error) {

    }
    return val
}

/**
* JS 计算两个时间间隔多久（时分秒）
* @param startTime "2019-10-23 15:27:23"
* @param endTime "2019-10-23 15:27:55"
* @return 1天2时3分5秒
*/
export function twoTimeInterval(startTime, endTime) {
    let d1, d2, date1, date2;
    if (typeof startTime == 'object') {
        date1 = startTime;
    } else {
        // 结束时间
        d1 = startTime.replace(/\-/g, "/");
        date1 = new Date(d1);
    }
    if (typeof endTime == 'object') {
        date2 = endTime;
    } else {
        // 结束时间
        d2 = endTime.replace(/\-/g, "/");
        date2 = new Date(d2);
    }

    // 时间相差秒数
    let dateDiff = date2.getTime() - date1.getTime();

    // 计算出相差天数
    let days = Math.floor(dateDiff / (24 * 3600 * 1000));

    // 计算出小时数
    let residue1 = dateDiff % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
    let hours = Math.floor(residue1 / (3600 * 1000));

    // 计算相差分钟数
    let residue2 = residue1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
    let minutes = Math.floor(residue2 / (60 * 1000));

    // 计算相差秒数
    let residue3 = residue2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
    let seconds = Math.round(residue3 / 1000);

    let returnVal =
        ((days == 0) ? "" : days + "天") +
        ((hours == 0) ? "" : days + ":") +
        ((minutes == 0) ? "" : minutes + ":") +
        ((seconds == 0) ? "" : seconds + ":");

    return returnVal;

}

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat 
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
    if (arguments.length === 0 || !time) {
        return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
        date = time
    } else {
        if ((typeof time === 'string')) {
            if ((/^[0-9]+$/.test(time))) {
                // support "1548221490638"
                time = parseInt(time)
            } else {
                // support safari
                // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
                time = time.replace(new RegExp(/-/gm), '/')
            }
        }

        if ((typeof time === 'number') && (time.toString().length === 10)) {
            time = time * 1000
        }
        date = new Date(time)
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    }
    const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
        const value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
        return value.toString().padStart(2, '0')
    })
    return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
    if (('' + time).length === 10) {
        time = parseInt(time) * 1000
    } else {
        time = +time
    }
    const d = new Date(time)
    const now = Date.now()

    const diff = (now - d) / 1000

    if (diff < 30) {
        return '刚刚'
    } else if (diff < 3600) {
        // less 1 hour
        return Math.ceil(diff / 60) + '分钟前'
    } else if (diff < 3600 * 24) {
        return Math.ceil(diff / 3600) + '小时前'
    } else if (diff < 3600 * 24 * 2) {
        return '1天前'
    }
    if (option) {
        return parseTime(time, option)
    } else {
        return (
            d.getMonth() +
            1 +
            '月' +
            d.getDate() +
            '日' +
            d.getHours() +
            '时' +
            d.getMinutes() +
            '分'
        )
    }
}


/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
    const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
    if (!search) {
        return {}
    }
    const obj = {}
    const searchArr = search.split('&')
    searchArr.forEach(v => {
        const index = v.indexOf('=')
        if (index !== -1) {
            const name = v.substring(0, index)
            const val = v.substring(index + 1, v.length)
            obj[name] = val
        }
    })
    return obj
}
